package edu.gettysburg.catapp1;

import java.io.File;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.support.v4.app.NavUtils;

/**
 * ChooseTour is the activity that gathers the downloaded tours, and displays a list to choose from
 * String[] tourChoices: private String array to hold list of tour files in directory
 * String TOUR_CHOICE: public String that indicates selected tour, title is carried to another activity
 */
public class choosetour extends Activity {
	private String[] tourChoices;
	public static String TOUR_CHOICE = "";

	/*
	 * (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 * Builds layout and list based on information from file directories
	 */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.choosetour_layout);
		getActionBar().setDisplayHomeAsUpEnabled(true);

		File tourHolder = getBaseContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);

		tourChoices = tourHolder.list();

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.menulist, tourChoices);
		ListView listView = (ListView) findViewById(R.id.tourlistView);
		listView.setAdapter(adapter);

		OnItemClickListener oicl = new OnItemClickListener(){

			/*
			 * (non-Javadoc)
			 * @see android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget.AdapterView, android.view.View, int, long)
			 * Has all items in the list act the same way: passes tour name to tour intro class to build from the correct file set
			 */
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				switch (position){  
				default:
					TOUR_CHOICE = tourChoices[position];
					Intent intent = new Intent(getBaseContext(), TourOverview.class);
					intent.putExtra(getPackageName(), TOUR_CHOICE);
					startActivity(intent);
					break;
				}
			}
		};
		listView.setOnItemClickListener(oicl);
	}

	/*
	 * (non-Javadoc)
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 * Creates standard options menu- one icon returns home by default
	 */
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.intro_layout, menu);
		return true;
	}


	/*
	 * (non-Javadoc)
	 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 * Returns to Home.java when home icon is pressed on action bar
	 */
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

}